//
//  NSMutableDictionary+BPCrash.m
//  BPCrashDemo
//
//  Created by SiYugui on 2019/12/18.
//  Copyright © 2019年 SiYugui. All rights reserved.
//

#import "NSMutableDictionary+BPCrash.h"
#import "NSObject+BPSwizzle.h"
#import "BPCrashLog.h"

@implementation NSMutableDictionary (BPCrash)
+(void)avoidMutableDictionaryCrashExchangeMethod{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class __dictionaryM = NSClassFromString(@"__NSDictionaryM");
        
        //setObject:forKey:
        [self exchangeInstanceMethod:__dictionaryM method1:@selector(setObject:forKey:) method2:@selector(__setObject:forKey:)];
        
        //setObject:forKeyedSubscript:
        if (@available(iOS 11.0, *)) {
            [self exchangeInstanceMethod:__dictionaryM method1:@selector(setObject:forKeyedSubscript:) method2:@selector(__setObject:forKeyedSubscript:)];
        }
        
        //removeObjectForKey:
        [self exchangeInstanceMethod:__dictionaryM method1:@selector(removeObjectForKey:) method2:@selector(__removeObjectForKey:)];
    });
}

#pragma mark - setObject:forKey:
-(void)__setObject:(id)anObject forKey:(id<NSCopying>)aKey{
    @try {
        [self __setObject:anObject forKey:aKey];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnIgnore];
    }
    @finally {
        
    }
}

#pragma mark - setObject:forKeyedSubscript:
-(void)__setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key{
    @try {
        [self __setObject:obj forKeyedSubscript:key];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnIgnore];
    }
    @finally {
        
    }
}

#pragma mark - removeObjectForKey:
-(void)__removeObjectForKey:(id)aKey{
    @try {
        [self removeObjectForKey:aKey];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnIgnore];
    }
    @finally {
        
    }
}
@end
